package model;

import javax.swing.JOptionPane;

/**
 * Used instead of the deprecated Date object
 * @author Eric
 *
 */
public class TimeObject {

	private static final int MIDNIGHT = 24;
	//private static final int TOP_OF_THE_HOUR = 60;
	private int my_start_hour;
	private int my_start_minute;
	private int my_duration;
	private int my_end_hour;
	private int my_end_minute;

	/**
	 * Construct a new TimeObject
	 * @param the_hour Hour represented in 0-23 time
	 * @param the_minute Minute in 0-59
	 * @param the_duration Duration may not be used unless desired
	 */
	public TimeObject(final int the_hour, final int the_minute,
					  final int the_duration) {
		my_start_hour = the_hour;
		my_start_minute = the_minute;
		my_duration = the_duration;
		calculateEndTime();
	}

	/**
	 * Changes the time of the auction by taking an start time and finding 
	 * the end time by calculating the duration.
	 */
	public void calculateEndTime() {

		// If the duration hours don't go passed midnight, add the time and temp hour and duration.
		if (my_start_hour + my_duration < MIDNIGHT) {
			my_end_hour = my_start_hour + my_duration;
			my_end_minute = my_start_minute;
		} else { // If the duration goes passed midnight, throw an exception.
			//TESTED by Kevin
			JOptionPane.showMessageDialog(null, "Duration goes past midnight",
				    "Invalid start time or duration",
				    JOptionPane.ERROR_MESSAGE);
		}
	}
	
	/**
	 * Start time in String hour:minute format
	 * @return
	 */
	public String getStartTime() {
		if(my_start_minute==0){
			return my_start_hour + ":" + "00";
		}else{
		return my_start_hour + ":" + my_start_minute;
		}
	}
	
	/**
	 * Set the start time
	 * @pre the_start_hour is between 0-23
	 * @pre the_start_minute is between 0-59
	 * 
	 * @param the_start_hour
	 * @param the_start_minute
	 */
	public void setStartTime(final int the_start_hour, final int the_start_minute) {
		my_start_hour = the_start_hour;
		my_start_minute = the_start_minute;
	}
	
	/**
	 * Possibly unused.  
	 * @return
	 */
	public int getDuration() {
		return my_duration;
	}
	
	/**
	 * Possibly unused.
	 * @param the_duration
	 */
	public void setDuration(final int the_duration) {
		my_duration = the_duration;
	}
	
	/**
	 * String of the ending time in hour:minute format
	 * @return
	 */
	public String getEndTime() {
		if(my_start_minute==0){
			return my_end_hour + ":" + "00";
		}else{
		return my_end_hour + ":" + my_end_minute;
		}
	}
	
	/**
	 * Set the desired ending time.
	 * @pre the_end_hour is between 0-23
	 * @pre the_end_minute is between 0-59
	 * 
	 * @param the_end_hour
	 * @param the_end_minute
	 */
	public void setEndTime(final int the_end_hour, final int the_end_minute) {
		my_end_hour = the_end_hour;
		my_end_minute = the_end_minute;
	}
	
	//****** Additional getters if needed:
	
	public int getStartHour() {
		return my_start_hour;
	}
	
	public int getStartMinute() {
		return my_start_minute;
	}
	
	public int getEndHour() {
		return my_end_hour;
	}
	
	public int getEndMinute() {
		return my_end_minute;
	}
	
	//******
	
	@Override
	public String toString() {
		return my_start_hour + ":" + my_start_minute + " to " + my_end_hour + ":" + my_end_minute;
	}

}
